package com.saleSystem.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.saleSystem.common.ServerResponse;
import com.saleSystem.dao.ContentMapper;
import com.saleSystem.dao.TradeHistoryMapper;
import com.saleSystem.pojo.Content;
import com.saleSystem.pojo.TradeHistory;
import com.saleSystem.service.ITradeHistoryService;
import com.saleSystem.vo.BuyProduct;

/**
 * 交易历史服务
 * @author unionpay
 *
 */
@Service("iTradeHistoryService")
public class TradeHistoryServiceImpl implements ITradeHistoryService {

	private static final Logger logger = LoggerFactory.getLogger(TradeHistoryServiceImpl.class);
	
	@Autowired
	private ContentMapper contentMapper;
	
	@Autowired
	private TradeHistoryMapper tradeHistoryMapper;
	
	
	@Transactional
	public ServerResponse<Boolean> insertTradeHistory(List<BuyProduct> buyList,Integer userId) {
		// TODO Auto-generated method stub
		if(userId == null || buyList== null || buyList.size()<=0){
			return ServerResponse.createByError("Buy fail！！",false);
		}
		List<TradeHistory> tradeHistoryList = new ArrayList<TradeHistory>();
		try {
			for(BuyProduct product : buyList){
				
				Content content = contentMapper.selectByPrimaryKey(Integer.valueOf(product.getId()));
				
				TradeHistory tradeHistory = new TradeHistory();
				tradeHistory.setBuynum(Integer.valueOf(product.getNumber()));
				tradeHistory.setBuyprice(content.getPrice());
				tradeHistory.setContentid(Integer.valueOf(product.getId()));
				tradeHistory.setTitle(content.getTitle());
				tradeHistory.setImage(content.getImage());
				tradeHistory.setUserid(userId);
				
				//购买时间
				Date buyTime = new Date(); 
				tradeHistory.setBuytime(buyTime);
				
				tradeHistoryList.add(tradeHistory);
			}
			
			try {
				tradeHistoryMapper.batchInsert(tradeHistoryList);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error("数据库批量插入产生异常！",e);
				return ServerResponse.createByError("Buy fail！！",false);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			logger.error("数据库查询产生异常！",e);
		}
		return ServerResponse.createBySuccess("Buy success！", true);
		
	}
	
	
	
	/**
	 * 获取产品的销售信息
	 */
	public TradeHistory getSellInfo(Integer contentid){
		
		if(contentid == null){
			return null;
		}
		List<TradeHistory> tradeHistoryList = new ArrayList<TradeHistory>();
		try {
			tradeHistoryList =	tradeHistoryMapper.selectByContentId(contentid);
			Integer buyNumSum = new Integer(0);
			if(tradeHistoryList != null && tradeHistoryList.size()>0){
				TradeHistory tradeHistory = tradeHistoryList.get(0);
				for(TradeHistory item : tradeHistoryList){
					buyNumSum += item.getBuynum();
				}
				tradeHistory.setBuynum(buyNumSum);
				return tradeHistory;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("数据库查询产生异常！",e);
		}
		return null;
	}
	
	/**
	 * 获取用户购买商品的信息
	 */
	public TradeHistory getBuyInfo(Integer userid,Integer contentid){
		
		if(userid == null || contentid == null){
			return null;
		}
		try {
			List<TradeHistory> tradeHistoryList = tradeHistoryMapper.selectByUserIdAndContentId(userid, contentid);
			
			if(tradeHistoryList != null && tradeHistoryList.size()>0){
				return tradeHistoryList.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("数据库查询产生异常！",e);
		}
		
		return null;
	}
	


	
	/**
	 * 获取当前用户的账务信息
	 */
	public List<TradeHistory> getAcountInfo(Integer userid) {
		// TODO Auto-generated method stub
		if(userid == null){
			return null;
		}
		List<TradeHistory> tradeHistoryList = new ArrayList<TradeHistory>();
		try {
			tradeHistoryList = tradeHistoryMapper.selectByUserId(userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("数据库查询产生异常！",e);
		}
		return tradeHistoryList;
	}
}
